package cn.innosmart.aq.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.AquariumBean;
import cn.innosmart.aq.bean.DeviceBean;
import cn.innosmart.aq.util.DeviceConstant;
import cn.innosmart.aq.util.SystemConstant;

/**
 * Created by David on 2015/8/11.
 */
public class HistoryDeviceListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context mContext;
    private ArrayList<String> historyItems;
    private AquariumBean aquariumBean;
    private View.OnClickListener listener;
    private boolean isModify;

    /**
     * 历史设备数据列表
     *
     * @param context
     */
    public HistoryDeviceListAdapter(Context context) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        historyItems = new ArrayList<String>();
    }

    @Override
    public int getCount() {
        return historyItems.size();
    }

    @Override
    public Object getItem(int position) {
        return historyItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.listview_device_list, null);
            holder.ivDeviceIcon = (ImageView) convertView.findViewById(R.id.iv_device_icon);
            holder.tvDeviceName = (TextView) convertView.findViewById(R.id.tv_device_name);
            holder.rl_modify = (RelativeLayout) convertView.findViewById(R.id.rl_modify);
            holder.iv_delete_device = (ImageView) convertView.findViewById(R.id.iv_delete_device);
            holder.iv_modify_device = (ImageView) convertView.findViewById(R.id.iv_modify_device);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        DeviceBean deviceBean = null;
        deviceBean = SystemConstant.deviceBeanHashMap.get(historyItems.get(position));
        if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_PH) {
            holder.ivDeviceIcon.setImageResource(R.drawable.ph_icon);
        } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_TEMPERATURE) {
            holder.ivDeviceIcon.setImageResource(R.drawable.temperature_icon);
        } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_SWITCH) {
            if (deviceBean.getUserTag() != null) {
                if (deviceBean.getUserTag().equals("Light Switch")) {
                    holder.ivDeviceIcon.setImageResource(R.drawable.switch_icon);
                } else if (deviceBean.getUserTag().equals("Feed Switch")) {
                    holder.ivDeviceIcon.setImageResource(R.drawable.feed_icon);
                } else if (deviceBean.getUserTag().equals("Water Switch")) {
                    holder.ivDeviceIcon.setImageResource(R.drawable.water_switch_icon);
                } else if (deviceBean.getUserTag().equals("O2 Switch")) {
                    holder.ivDeviceIcon.setImageResource(R.drawable.switch_icon);
                } else {
                    holder.ivDeviceIcon.setImageResource(R.drawable.switch_icon);
                }
            } else {
                holder.ivDeviceIcon.setImageResource(R.drawable.switch_icon);
            }
        } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_WATERLEVER) {
            holder.ivDeviceIcon.setImageResource(R.drawable.water_lever_icon);
        } else if (deviceBean.getFunctype() == DeviceConstant.FUNCTYPE_FEEDER) {
            holder.ivDeviceIcon.setImageResource(R.drawable.feed_icon);
        } else {
            holder.ivDeviceIcon.setImageResource(R.drawable.ictest);
        }

        if (deviceBean != null) {
            String tag = deviceBean.getUserTag();
            if (tag != null) {
                if (DeviceConstant.nameMap.get(tag) == null) {
                    holder.tvDeviceName.setText(tag);
                } else {
                    holder.tvDeviceName.setText(DeviceConstant.nameMap.get(tag));
                }
            } else {
                holder.tvDeviceName.setText(deviceBean.getName());
            }
        }
        if (isModify) {
            holder.rl_modify.setVisibility(View.VISIBLE);
            holder.iv_modify_device.setVisibility(View.GONE);
            holder.ivDeivceInfo.setVisibility(View.GONE);
            holder.iv_delete_device.setOnClickListener(listener);
            holder.iv_modify_device.setOnClickListener(listener);
        } else {
            holder.rl_modify.setVisibility(View.GONE);
            holder.ivDeivceInfo.setVisibility(View.VISIBLE);
            holder.ivDeivceInfo.setOnClickListener(listener);
        }
        return convertView;
    }

    /**
     * 设置列表点击事件监听器
     *
     * @param listviewOnClickListener
     */
    public void setOnclickListener(View.OnClickListener listviewOnClickListener) {
        listener = listviewOnClickListener;
    }

    /**
     * 进入修改模式
     *
     * @param IsModify
     */
    public void startModify(Boolean IsModify) {
        isModify = IsModify;
        notifyDataSetChanged();
    }

    /**
     * 刷新数据
     * @param aquariumBean
     */
    public void refreshData(AquariumBean aquariumBean) {
        this.aquariumBean = aquariumBean;
        historyItems = (ArrayList<String>) aquariumBean.getDeivices().clone();
        notifyDataSetChanged();
    }

    static class ViewHolder {
        ImageView ivDeviceIcon;
        TextView tvDeviceName;
        ImageView ivDeivceInfo;
        RelativeLayout rl_modify;
        ImageView iv_delete_device;
        ImageView iv_modify_device;
    }
}
